import wx
import copy

from CustSearchCtrl import CustSearchCtrl


class DiaTableFliter(wx.Dialog):
    def __init__(self, parent, pos=wx.DefaultPosition, *args, **kwargs):
        super().__init__(parent, -1, '', pos=pos, style=wx.DIALOG_NO_PARENT)

        self.Data = kwargs.get('data')
        self.Data = list(set(self.Data))
        self.NewData = copy.deepcopy(self.Data)
        self.NewData = list(set(self.NewData))
        self.Data.sort()
        self.NewData.sort()

        self.SearchCtrl = CustSearchCtrl(self)
        self.ListCtrl = wx.ListCtrl(
            self, -1, style=wx.LC_REPORT, size=(-1, 500))

        self.BtnSave = wx.Button(self, -1, '确定')
        self.BtnCancel = wx.Button(self, -1, '取消')

        Sizer = wx.BoxSizer(wx.VERTICAL)
        BtnSizer = wx.BoxSizer(wx.HORIZONTAL)

        BtnSizer.Add((150, -1), 0)
        BtnSizer.Add(self.BtnSave, 0, wx.EXPAND, 5)
        BtnSizer.Add(self.BtnCancel, 0, wx.EXPAND, 5)
        Sizer.Add(self.SearchCtrl, 0, wx.EXPAND, 5)
        Sizer.Add(self.ListCtrl, 0, wx.EXPAND, 5)
        Sizer.Add(BtnSizer, 0, wx.EXPAND, 5)

        self.SetSizer(Sizer)
        self.Fit()

        self.InitList(self.Data)

        self.ListCtrl.Bind(wx.EVT_LIST_ITEM_CHECKED, self.OnChecked)
        self.ListCtrl.Bind(wx.EVT_LIST_ITEM_UNCHECKED, self.OnUnChecked)
        self.SearchCtrl.Bind(wx.EVT_TEXT, self.OnSearch)
        self.Bind(wx.EVT_BUTTON, self.OnSave, self.BtnSave)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, self.BtnCancel)
        self.CheckAll(0)

    def OnSave(self, event):
        self.HideRows(self.NewData, self.Col)

    def OnCancel(self, event):
        self.OnActive(0)
    
    def GetNewData(self):
        return copy.deepcopy(self.NewData)

    def InitList(self, data):
        self.ListCtrl.ClearAll()
        data = copy.deepcopy(data)
        self.ListCtrl.EnableCheckBoxes(True)
        for col, name in enumerate(['选择', '名称']):
            self.ListCtrl.InsertColumn(col, name)
        for col, name in enumerate([40, 240]):
            self.ListCtrl.SetColumnWidth(col, name)

        self.AllItem = self.ListCtrl.InsertItem(0, '')
        self.ListCtrl.SetItem(0, 1, '全选')

        for index, d in enumerate(data):
            self.ListCtrl.InsertItem(index+1, '')
            self.ListCtrl.SetItem(index+1, 1, str(d))

    def CheckAll(self, event):
        Obj = self.ListCtrl
        for i in range(Obj.GetItemCount()):
            Obj.CheckItem(i)

    def OnChecked(self, event):
        Obj = event.GetEventObject()
        Index = event.GetIndex()
        if Index == 0:
            for i in range(1, Obj.GetItemCount()):
                Obj.CheckItem(i)
                Data = self.ListCtrl.GetItemText(i, 1)
                if Data not in self.NewData:
                    self.NewData.append(Data)
            self.NewData.sort()
        else:
            Data = self.ListCtrl.GetItemText(Index, 1)
            if Data not in self.NewData:
                self.NewData.append(Data)
                self.NewData.sort()
        

    def OnUnChecked(self, event):
        Obj = event.GetEventObject()
        Index = event.GetIndex()
        if Index == 0:
            for i in range(1, Obj.GetItemCount()):
                Obj.CheckItem(i, False)
                Data = self.ListCtrl.GetItemText(i, 1)
                if Data in self.NewData:
                    self.NewData.remove(Data)
            self.NewData.sort()
        else:
            Data = self.ListCtrl.GetItemText(Index, 1)
            if Data in self.NewData:
                self.NewData.remove(Data)
                self.NewData.sort()

    def OnSearch(self, event):
        Data = copy.deepcopy(self.Data)
        self.NewData = []
        for d in Data:
            if self.SearchCtrl.IsCheckCapital:
                SCValue = self.SearchCtrl.GetValue()
                CellData = d
            else:
                SCValue = str(self.SearchCtrl.GetValue()).upper()
                CellData = str(d).upper()

            if self.SearchCtrl.IsCheckAll:
                if SCValue == CellData:
                    MatchResult = True
                else:
                    MatchResult = False
            else:
                if SCValue in CellData:
                    MatchResult = True
                else:
                    MatchResult = False

            if MatchResult:
                self.NewData.append(d)
        self.InitList(self.NewData)
        self.CheckAll(0)


if __name__ == "__main__":
    Data = []
    for index, i in enumerate(range(2, 5)):
        Data.append('测试' + str(i//2))

    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(10, 10))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)
            btn.Bind(wx.EVT_SET_FOCUS, self.OnDestroyDia)

        def OnClick(self, event):
            # modal = DiaTable2add(self, 'baba').ShowModal()
            self.dia = DiaTableFliter(self, data=Data)
            self.dia.Show()
            # print(modal)
            # dia.Destroy()

        def OnDestroyDia(self, event):
            self.dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()
